#include <QtCore/QCoreApplication>
#include <string>
#include <QString>
 #include <QStringList>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <QtXml>
//#include <qdom.h>

#define IO_ReadOnly QIODevice::ReadOnly
#define IO_WriteOnly QIODevice::WriteOnly
#define IO_ReadWrite QIODevice::ReadWrite
#define IO_Append QIODevice::Append
#define IO_Truncate QIODevice::Truncate
#define IO_Translate QIODevice::Text
#define IO_ModeMask 0x00ff

int ConvertTimestamp(int hours, int minutes, int secondes);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
	QDomDocument dDoc( "mXML" );
	/*QDomElement mRoot = dDoc.createElement( "mRoot" );
	dDoc.appendChild( mRoot );
	QDomElement timeline = dDoc.createElement( "timeline" );
	timeline.setAttribute( "File", "movie1.avi" );
	timeline.setAttribute( "Timestamp", 0 );
	timeline.setAttribute( "Size", "320x640" );
	mRoot.appendChild( timeline );
	QDomElement timeline2 = dDoc.createElement( "timeline" );
	timeline2.setAttribute( "File", "movie2.mpg" );
	timeline2.setAttribute( "Timestamp", 10 );
	timeline2.setAttribute( "Size", "280x320" );
	mRoot.appendChild( timeline2 );
	QFile file( "test.xml" );
	if ( !file.open( IO_WriteOnly ) )
	{
		std::cout << "Couldn't write FILE!!!";
	}
	else
	{
		QTextStream ts( &file );
		ts << dDoc.toString();
		file.close();
	}*/
	QFile file( "test.xml" );
	if ( !file.open( IO_ReadOnly ) )
	{
		std::cout << "Couldn't read FILE!!!";
	}
	else
	{
		if ( !dDoc.setContent( &file ) )
		{
			std::cout << "Couldn't set CONTENT!!!";
			file.close();
		}
		file.close();
		QDomElement mRoot = dDoc.documentElement();
		if ( mRoot.tagName() != "mRoot" )
		{
			std::cout << "Tag wasn't mRoot!!!";
		}
		else
		{
			QDomNode n = mRoot.firstChild();
			while( !n.isNull() )
			{
				QDomElement e = n.toElement();
				if( !e.isNull() )
				{
					if( e.tagName() == "video" )
					{
						QString AttFile = e.attribute( "File", "" );
						std::cout << "\nsrc : " << e.attribute( "src", "no file" ).toStdString() << std::endl << "Begin : " << e.attribute( "begin", 0 ).toStdString() << std::endl << "End: " << e.attribute( "end", 0 ).toStdString();
						QStringList list1 = e.attribute( "begin", 0 ).split(":");
						int seconds = ConvertTimestamp(list1.at(0).toInt(), list1.at(1).toInt(), list1.at(2).toInt());

						std::cout << std::endl << seconds << std::endl;
					}
				}
				n = n.nextSibling();
			}
		}
	}
	std::cout << std::endl;
	system("pause");
    return a.exit();
}

int ConvertTimestamp(int hours, int minutes, int secondes)
{
	while(hours > 0)
	{
		minutes = minutes + 60;
		hours--;
	}
	while(minutes > 0)
	{
		secondes = secondes + 60;
		minutes--;
	}
	return secondes;
}